

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Building LLVM with CMake &mdash; LLVM 3.2 documentation</title>
    
    <link rel="stylesheet" href="_static/llvm-theme.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '',
        VERSION:     '3.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="top" title="LLVM 3.2 documentation" href="index.html" />
    <link rel="up" title="User Guides" href="userguides.html" />
    <link rel="next" title="How To Build On ARM" href="HowToBuildOnARM.html" />
    <link rel="prev" title="User Guides" href="userguides.html" />
<style type="text/css">
  table.right { float: right; margin-left: 20px; }
  table.right td { border: 1px solid #ccc; }
</style>

  </head>
  <body>
<div class="logo">
  <a href="index.html">
    <img src="_static/logo.png"
         alt="LLVM Logo" width="250" height="88"/></a>
</div>

    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="HowToBuildOnARM.html" title="How To Build On ARM"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="userguides.html" title="User Guides"
             accesskey="P">previous</a> |</li>
  <li><a href="http://llvm.org/">LLVM Home</a>&nbsp;|&nbsp;</li>
  <li><a href="index.html">Documentation</a>&raquo;</li>

          <li><a href="userguides.html" accesskey="U">User Guides</a> &raquo;</li> 
      </ul>
    </div>

      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="index.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Building LLVM with CMake</a><ul>
<li><a class="reference internal" href="#introduction">Introduction</a></li>
<li><a class="reference internal" href="#quick-start">Quick start</a></li>
<li><a class="reference internal" href="#usage">Basic CMake usage</a></li>
<li><a class="reference internal" href="#options-and-variables">Options and variables</a><ul>
<li><a class="reference internal" href="#frequently-used-cmake-variables">Frequently-used CMake variables</a></li>
<li><a class="reference internal" href="#llvm-specific-variables">LLVM-specific variables</a></li>
</ul>
</li>
<li><a class="reference internal" href="#executing-the-test-suite">Executing the test suite</a></li>
<li><a class="reference internal" href="#cross-compiling">Cross compiling</a></li>
<li><a class="reference internal" href="#embedding-llvm-in-your-project">Embedding LLVM in your project</a><ul>
<li><a class="reference internal" href="#developing-llvm-pass-out-of-source">Developing LLVM pass out of source</a></li>
</ul>
</li>
<li><a class="reference internal" href="#compiler-platform-specific-topics">Compiler/Platform specific topics</a><ul>
<li><a class="reference internal" href="#microsoft-visual-c">Microsoft Visual C++</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="userguides.html"
                        title="previous chapter">User Guides</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="HowToBuildOnARM.html"
                        title="next chapter">How To Build On ARM</a></p>
  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="_sources/CMake.txt"
           rel="nofollow">Show Source</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="building-llvm-with-cmake">
<span id="building-with-cmake"></span><h1>Building LLVM with CMake<a class="headerlink" href="#building-llvm-with-cmake" title="Permalink to this headline">¶</a></h1>
<div class="contents local topic" id="contents">
<ul class="simple">
<li><a class="reference internal" href="#introduction" id="id5">Introduction</a></li>
<li><a class="reference internal" href="#quick-start" id="id6">Quick start</a></li>
<li><a class="reference internal" href="#usage" id="id7">Basic CMake usage</a></li>
<li><a class="reference internal" href="#options-and-variables" id="id8">Options and variables</a><ul>
<li><a class="reference internal" href="#frequently-used-cmake-variables" id="id9">Frequently-used CMake variables</a></li>
<li><a class="reference internal" href="#llvm-specific-variables" id="id10">LLVM-specific variables</a></li>
</ul>
</li>
<li><a class="reference internal" href="#executing-the-test-suite" id="id11">Executing the test suite</a></li>
<li><a class="reference internal" href="#cross-compiling" id="id12">Cross compiling</a></li>
<li><a class="reference internal" href="#embedding-llvm-in-your-project" id="id13">Embedding LLVM in your project</a><ul>
<li><a class="reference internal" href="#developing-llvm-pass-out-of-source" id="id14">Developing LLVM pass out of source</a></li>
</ul>
</li>
<li><a class="reference internal" href="#compiler-platform-specific-topics" id="id15">Compiler/Platform specific topics</a><ul>
<li><a class="reference internal" href="#microsoft-visual-c" id="id16">Microsoft Visual C++</a></li>
</ul>
</li>
</ul>
</div>
<div class="section" id="introduction">
<h2><a class="toc-backref" href="#id5">Introduction</a><a class="headerlink" href="#introduction" title="Permalink to this headline">¶</a></h2>
<p><a class="reference external" href="http://www.cmake.org/">CMake</a> is a cross-platform build-generator tool. CMake
does not build the project, it generates the files needed by your build tool
(GNU make, Visual Studio, etc) for building LLVM.</p>
<p>If you are really anxious about getting a functional LLVM build, go to the
<a class="reference internal" href="#quick-start">Quick start</a> section. If you are a CMake novice, start on <a class="reference internal" href="#basic-cmake-usage">Basic CMake usage</a>
and then go back to the <a class="reference internal" href="#quick-start">Quick start</a> once you know what you are doing. The
<a class="reference internal" href="#options-and-variables">Options and variables</a> section is a reference for customizing your build. If
you already have experience with CMake, this is the recommended starting point.</p>
</div>
<div class="section" id="quick-start">
<span id="id1"></span><h2><a class="toc-backref" href="#id6">Quick start</a><a class="headerlink" href="#quick-start" title="Permalink to this headline">¶</a></h2>
<p>We use here the command-line, non-interactive CMake interface.</p>
<ol class="arabic">
<li><p class="first"><a class="reference external" href="http://www.cmake.org/cmake/resources/software.html">Download</a> and install
CMake. Version 2.8 is the minimum required.</p>
</li>
<li><p class="first">Open a shell. Your development tools must be reachable from this shell
through the PATH environment variable.</p>
</li>
<li><p class="first">Create a directory for containing the build. It is not supported to build
LLVM on the source directory. cd to this directory:</p>
<div class="highlight-bash"><div class="highlight"><pre><span class="nv">$ </span>mkdir mybuilddir
<span class="nv">$ </span><span class="nb">cd </span>mybuilddir
</pre></div>
</div>
</li>
<li><p class="first">Execute this command on the shell replacing <cite>path/to/llvm/source/root</cite> with
the path to the root of your LLVM source tree:</p>
<div class="highlight-bash"><div class="highlight"><pre><span class="nv">$ </span>cmake path/to/llvm/source/root
</pre></div>
</div>
<p>CMake will detect your development environment, perform a series of test and
generate the files required for building LLVM. CMake will use default values
for all build parameters. See the <a class="reference internal" href="#options-and-variables">Options and variables</a> section for
fine-tuning your build</p>
<p>This can fail if CMake can&#8217;t detect your toolset, or if it thinks that the
environment is not sane enough. On this case make sure that the toolset that
you intend to use is the only one reachable from the shell and that the shell
itself is the correct one for you development environment. CMake will refuse
to build MinGW makefiles if you have a POSIX shell reachable through the PATH
environment variable, for instance. You can force CMake to use a given build
tool, see the <a class="reference internal" href="#usage">Usage</a> section.</p>
</li>
</ol>
</div>
<div class="section" id="usage">
<span id="basic-cmake-usage"></span><span id="id2"></span><h2><a class="toc-backref" href="#id7">Basic CMake usage</a><a class="headerlink" href="#usage" title="Permalink to this headline">¶</a></h2>
<p>This section explains basic aspects of CMake, mostly for explaining those
options which you may need on your day-to-day usage.</p>
<p>CMake comes with extensive documentation in the form of html files and on the
cmake executable itself. Execute <tt class="docutils literal"><span class="pre">cmake</span> <span class="pre">--help</span></tt> for further help options.</p>
<p>CMake requires to know for which build tool it shall generate files (GNU make,
Visual Studio, Xcode, etc). If not specified on the command line, it tries to
guess it based on you environment. Once identified the build tool, CMake uses
the corresponding <em>Generator</em> for creating files for your build tool. You can
explicitly specify the generator with the command line option <tt class="docutils literal"><span class="pre">-G</span> <span class="pre">&quot;Name</span> <span class="pre">of</span> <span class="pre">the</span>
<span class="pre">generator&quot;</span></tt>. For knowing the available generators on your platform, execute</p>
<div class="highlight-bash"><div class="highlight"><pre><span class="nv">$ </span>cmake --help
</pre></div>
</div>
<p>This will list the generator&#8217;s names at the end of the help text. Generator&#8217;s
names are case-sensitive. Example:</p>
<div class="highlight-bash"><div class="highlight"><pre><span class="nv">$ </span>cmake -G <span class="s2">&quot;Visual Studio 9 2008&quot;</span> path/to/llvm/source/root
</pre></div>
</div>
<p>For a given development platform there can be more than one adequate
generator. If you use Visual Studio &#8220;NMake Makefiles&#8221; is a generator you can use
for building with NMake. By default, CMake chooses the more specific generator
supported by your development environment. If you want an alternative generator,
you must tell this to CMake with the <tt class="docutils literal"><span class="pre">-G</span></tt> option.</p>
</div>
<div class="section" id="options-and-variables">
<span id="id3"></span><h2><a class="toc-backref" href="#id8">Options and variables</a><a class="headerlink" href="#options-and-variables" title="Permalink to this headline">¶</a></h2>
<p>Variables customize how the build will be generated. Options are boolean
variables, with possible values ON/OFF. Options and variables are defined on the
CMake command line like this:</p>
<div class="highlight-bash"><div class="highlight"><pre><span class="nv">$ </span>cmake -DVARIABLE<span class="o">=</span>value path/to/llvm/source
</pre></div>
</div>
<p>You can set a variable after the initial CMake invocation for changing its
value. You can also undefine a variable:</p>
<div class="highlight-bash"><div class="highlight"><pre><span class="nv">$ </span>cmake -UVARIABLE path/to/llvm/source
</pre></div>
</div>
<p>Variables are stored on the CMake cache. This is a file named <tt class="docutils literal"><span class="pre">CMakeCache.txt</span></tt>
on the root of the build directory. Do not hand-edit it.</p>
<p>Variables are listed here appending its type after a colon. It is correct to
write the variable and the type on the CMake command line:</p>
<div class="highlight-bash"><div class="highlight"><pre><span class="nv">$ </span>cmake -DVARIABLE:TYPE<span class="o">=</span>value path/to/llvm/source
</pre></div>
</div>
<div class="section" id="frequently-used-cmake-variables">
<h3><a class="toc-backref" href="#id9">Frequently-used CMake variables</a><a class="headerlink" href="#frequently-used-cmake-variables" title="Permalink to this headline">¶</a></h3>
<p>Here are listed some of the CMake variables that are used often, along with a
brief explanation and LLVM-specific notes. For full documentation, check the
CMake docs or execute <tt class="docutils literal"><span class="pre">cmake</span> <span class="pre">--help-variable</span> <span class="pre">VARIABLE_NAME</span></tt>.</p>
<dl class="docutils">
<dt><strong>CMAKE_BUILD_TYPE</strong>:STRING</dt>
<dd>Sets the build type for <tt class="docutils literal"><span class="pre">make</span></tt> based generators. Possible values are
Release, Debug, RelWithDebInfo and MinSizeRel. On systems like Visual Studio
the user sets the build type with the IDE settings.</dd>
<dt><strong>CMAKE_INSTALL_PREFIX</strong>:PATH</dt>
<dd>Path where LLVM will be installed if &#8220;make install&#8221; is invoked or the
&#8220;INSTALL&#8221; target is built.</dd>
<dt><strong>LLVM_LIBDIR_SUFFIX</strong>:STRING</dt>
<dd>Extra suffix to append to the directory where libraries are to be
installed. On a 64-bit architecture, one could use <tt class="docutils literal"><span class="pre">-DLLVM_LIBDIR_SUFFIX=64</span></tt>
to install libraries to <tt class="docutils literal"><span class="pre">/usr/lib64</span></tt>.</dd>
<dt><strong>CMAKE_C_FLAGS</strong>:STRING</dt>
<dd>Extra flags to use when compiling C source files.</dd>
<dt><strong>CMAKE_CXX_FLAGS</strong>:STRING</dt>
<dd>Extra flags to use when compiling C++ source files.</dd>
<dt><strong>BUILD_SHARED_LIBS</strong>:BOOL</dt>
<dd>Flag indicating is shared libraries will be built. Its default value is
OFF. Shared libraries are not supported on Windows and not recommended in the
other OSes.</dd>
</dl>
</div>
<div class="section" id="llvm-specific-variables">
<span id="id4"></span><h3><a class="toc-backref" href="#id10">LLVM-specific variables</a><a class="headerlink" href="#llvm-specific-variables" title="Permalink to this headline">¶</a></h3>
<dl class="docutils">
<dt><strong>LLVM_TARGETS_TO_BUILD</strong>:STRING</dt>
<dd>Semicolon-separated list of targets to build, or <em>all</em> for building all
targets. Case-sensitive. For Visual C++ defaults to <em>X86</em>. On the other cases
defaults to <em>all</em>. Example: <tt class="docutils literal"><span class="pre">-DLLVM_TARGETS_TO_BUILD=&quot;X86;PowerPC&quot;</span></tt>.</dd>
<dt><strong>LLVM_BUILD_TOOLS</strong>:BOOL</dt>
<dd>Build LLVM tools. Defaults to ON. Targets for building each tool are generated
in any case. You can build an tool separately by invoking its target. For
example, you can build <em>llvm-as</em> with a makefile-based system executing <em>make
llvm-as</em> on the root of your build directory.</dd>
<dt><strong>LLVM_INCLUDE_TOOLS</strong>:BOOL</dt>
<dd>Generate build targets for the LLVM tools. Defaults to ON. You can use that
option for disabling the generation of build targets for the LLVM tools.</dd>
<dt><strong>LLVM_BUILD_EXAMPLES</strong>:BOOL</dt>
<dd>Build LLVM examples. Defaults to OFF. Targets for building each example are
generated in any case. See documentation for <em>LLVM_BUILD_TOOLS</em> above for more
details.</dd>
<dt><strong>LLVM_INCLUDE_EXAMPLES</strong>:BOOL</dt>
<dd>Generate build targets for the LLVM examples. Defaults to ON. You can use that
option for disabling the generation of build targets for the LLVM examples.</dd>
<dt><strong>LLVM_BUILD_TESTS</strong>:BOOL</dt>
<dd>Build LLVM unit tests. Defaults to OFF. Targets for building each unit test
are generated in any case. You can build a specific unit test with the target
<em>UnitTestNameTests</em> (where at this time <em>UnitTestName</em> can be ADT, Analysis,
ExecutionEngine, JIT, Support, Transform, VMCore; see the subdirectories of
<em>unittests</em> for an updated list.) It is possible to build all unit tests with
the target <em>UnitTests</em>.</dd>
<dt><strong>LLVM_INCLUDE_TESTS</strong>:BOOL</dt>
<dd>Generate build targets for the LLVM unit tests. Defaults to ON. You can use
that option for disabling the generation of build targets for the LLVM unit
tests.</dd>
<dt><strong>LLVM_APPEND_VC_REV</strong>:BOOL</dt>
<dd>Append version control revision info (svn revision number or git revision id)
to LLVM version string (stored in the PACKAGE_VERSION macro). For this to work
cmake must be invoked before the build. Defaults to OFF.</dd>
<dt><strong>LLVM_ENABLE_THREADS</strong>:BOOL</dt>
<dd>Build with threads support, if available. Defaults to ON.</dd>
<dt><strong>LLVM_ENABLE_ASSERTIONS</strong>:BOOL</dt>
<dd>Enables code assertions. Defaults to OFF if and only if <tt class="docutils literal"><span class="pre">CMAKE_BUILD_TYPE</span></tt>
is <em>Release</em>.</dd>
<dt><strong>LLVM_ENABLE_PIC</strong>:BOOL</dt>
<dd>Add the <tt class="docutils literal"><span class="pre">-fPIC</span></tt> flag for the compiler command-line, if the compiler supports
this flag. Some systems, like Windows, do not need this flag. Defaults to ON.</dd>
<dt><strong>LLVM_ENABLE_WARNINGS</strong>:BOOL</dt>
<dd>Enable all compiler warnings. Defaults to ON.</dd>
<dt><strong>LLVM_ENABLE_PEDANTIC</strong>:BOOL</dt>
<dd>Enable pedantic mode. This disable compiler specific extensions, is
possible. Defaults to ON.</dd>
<dt><strong>LLVM_ENABLE_WERROR</strong>:BOOL</dt>
<dd>Stop and fail build, if a compiler warning is triggered. Defaults to OFF.</dd>
<dt><strong>LLVM_BUILD_32_BITS</strong>:BOOL</dt>
<dd>Build 32-bits executables and libraries on 64-bits systems. This option is
available only on some 64-bits unix systems. Defaults to OFF.</dd>
<dt><strong>LLVM_TARGET_ARCH</strong>:STRING</dt>
<dd>LLVM target to use for native code generation. This is required for JIT
generation. It defaults to &#8220;host&#8221;, meaning that it shall pick the architecture
of the machine where LLVM is being built. If you are cross-compiling, set it
to the target architecture name.</dd>
<dt><strong>LLVM_TABLEGEN</strong>:STRING</dt>
<dd>Full path to a native TableGen executable (usually named <tt class="docutils literal"><span class="pre">tblgen</span></tt>). This is
intended for cross-compiling: if the user sets this variable, no native
TableGen will be created.</dd>
<dt><strong>LLVM_LIT_ARGS</strong>:STRING</dt>
<dd>Arguments given to lit.  <tt class="docutils literal"><span class="pre">make</span> <span class="pre">check</span></tt> and <tt class="docutils literal"><span class="pre">make</span> <span class="pre">clang-test</span></tt> are affected.
By default, <tt class="docutils literal"><span class="pre">'-sv</span> <span class="pre">--no-progress-bar'</span></tt> on Visual C++ and Xcode, <tt class="docutils literal"><span class="pre">'-sv'</span></tt> on
others.</dd>
<dt><strong>LLVM_LIT_TOOLS_DIR</strong>:PATH</dt>
<dd>The path to GnuWin32 tools for tests. Valid on Windows host.  Defaults to &#8220;&#8221;,
then Lit seeks tools according to %PATH%.  Lit can find tools(eg. grep, sort,
&amp;c) on LLVM_LIT_TOOLS_DIR at first, without specifying GnuWin32 to %PATH%.</dd>
<dt><strong>LLVM_ENABLE_FFI</strong>:BOOL</dt>
<dd>Indicates whether LLVM Interpreter will be linked with Foreign Function
Interface library. If the library or its headers are installed on a custom
location, you can set the variables FFI_INCLUDE_DIR and
FFI_LIBRARY_DIR. Defaults to OFF.</dd>
<dt><strong>LLVM_EXTERNAL_{CLANG,LLD,POLLY}_SOURCE_DIR</strong>:PATH</dt>
<dd>Path to <tt class="docutils literal"><span class="pre">{Clang,lld,Polly}</span></tt>&#8216;s source directory. Defaults to
<tt class="docutils literal"><span class="pre">tools/{clang,lld,polly}</span></tt>. <tt class="docutils literal"><span class="pre">{Clang,lld,Polly}</span></tt> will not be built when it
is empty or it does not point valid path.</dd>
<dt><strong>LLVM_USE_OPROFILE</strong>:BOOL</dt>
<dd>Enable building OProfile JIT support. Defaults to OFF</dd>
<dt><strong>LLVM_USE_INTEL_JITEVENTS</strong>:BOOL</dt>
<dd>Enable building support for Intel JIT Events API. Defaults to OFF</dd>
</dl>
</div>
</div>
<div class="section" id="executing-the-test-suite">
<h2><a class="toc-backref" href="#id11">Executing the test suite</a><a class="headerlink" href="#executing-the-test-suite" title="Permalink to this headline">¶</a></h2>
<p>Testing is performed when the <em>check</em> target is built. For instance, if you are
using makefiles, execute this command while on the top level of your build
directory:</p>
<div class="highlight-bash"><div class="highlight"><pre><span class="nv">$ </span>make check
</pre></div>
</div>
<p>On Visual Studio, you may run tests to build the project &#8220;check&#8221;.</p>
</div>
<div class="section" id="cross-compiling">
<h2><a class="toc-backref" href="#id12">Cross compiling</a><a class="headerlink" href="#cross-compiling" title="Permalink to this headline">¶</a></h2>
<p>See <a class="reference external" href="http://www.vtk.org/Wiki/CMake_Cross_Compiling">this wiki page</a> for
generic instructions on how to cross-compile with CMake. It goes into detailed
explanations and may seem daunting, but it is not. On the wiki page there are
several examples including toolchain files. Go directly to <a class="reference external" href="http://www.vtk.org/Wiki/CMake_Cross_Compiling#Information_how_to_set_up_various_cross_compiling_toolchains">this section</a>
for a quick solution.</p>
<p>Also see the <a class="reference internal" href="#llvm-specific-variables">LLVM-specific variables</a> section for variables used when
cross-compiling.</p>
</div>
<div class="section" id="embedding-llvm-in-your-project">
<h2><a class="toc-backref" href="#id13">Embedding LLVM in your project</a><a class="headerlink" href="#embedding-llvm-in-your-project" title="Permalink to this headline">¶</a></h2>
<p>The most difficult part of adding LLVM to the build of a project is to determine
the set of LLVM libraries corresponding to the set of required LLVM
features. What follows is an example of how to obtain this information:</p>
<div class="highlight-cmake"><div class="highlight"><pre><span class="c"># A convenience variable:</span>
<span class="nb">set</span><span class="p">(</span><span class="s">LLVM_ROOT</span> <span class="s2">&quot;&quot;</span> <span class="s">CACHE</span> <span class="s">PATH</span> <span class="s2">&quot;Root of LLVM install.&quot;</span><span class="p">)</span>

<span class="c"># A bit of a sanity check:</span>
<span class="nb">if</span><span class="p">(</span> <span class="s">NOT</span> <span class="s">EXISTS</span> <span class="o">${</span><span class="nv">LLVM_ROOT</span><span class="o">}</span><span class="s">/include/llvm</span> <span class="p">)</span>
<span class="nb">message</span><span class="p">(</span><span class="s">FATAL_ERROR</span> <span class="s2">&quot;LLVM_ROOT (${LLVM_ROOT}) is not a valid LLVM install&quot;</span><span class="p">)</span>
<span class="nb">endif</span><span class="p">()</span>

<span class="c"># We incorporate the CMake features provided by LLVM:</span>
<span class="nb">set</span><span class="p">(</span><span class="s">CMAKE_MODULE_PATH</span> <span class="o">${</span><span class="nv">CMAKE_MODULE_PATH</span><span class="o">}</span> <span class="s2">&quot;${LLVM_ROOT}/share/llvm/cmake&quot;</span><span class="p">)</span>
<span class="nb">include</span><span class="p">(</span><span class="s">LLVMConfig</span><span class="p">)</span>

<span class="c"># Now set the header and library paths:</span>
<span class="nb">include_directories</span><span class="p">(</span> <span class="o">${</span><span class="nv">LLVM_INCLUDE_DIRS</span><span class="o">}</span> <span class="p">)</span>
<span class="nb">link_directories</span><span class="p">(</span> <span class="o">${</span><span class="nv">LLVM_LIBRARY_DIRS</span><span class="o">}</span> <span class="p">)</span>
<span class="nb">add_definitions</span><span class="p">(</span> <span class="o">${</span><span class="nv">LLVM_DEFINITIONS</span><span class="o">}</span> <span class="p">)</span>

<span class="c"># Let&#39;s suppose we want to build a JIT compiler with support for</span>
<span class="c"># binary code (no interpreter):</span>
<span class="nb">llvm_map_components_to_libraries</span><span class="p">(</span><span class="s">REQ_LLVM_LIBRARIES</span> <span class="s">jit</span> <span class="s">native</span><span class="p">)</span>

<span class="c"># Finally, we link the LLVM libraries to our executable:</span>
<span class="nb">target_link_libraries</span><span class="p">(</span><span class="s">mycompiler</span> <span class="o">${</span><span class="nv">REQ_LLVM_LIBRARIES</span><span class="o">}</span><span class="p">)</span>
</pre></div>
</div>
<p>This assumes that LLVM_ROOT points to an install of LLVM. The procedure works
too for uninstalled builds although we need to take care to add an
<cite>include_directories</cite> for the location of the headers on the LLVM source
directory (if we are building out-of-source.)</p>
<p>Alternativaly, you can utilize CMake&#8217;s <tt class="docutils literal"><span class="pre">find_package</span></tt> functionality. Here is
an equivalent variant of snippet shown above:</p>
<div class="highlight-cmake"><div class="highlight"><pre><span class="nb">find_package</span><span class="p">(</span><span class="s">LLVM</span><span class="p">)</span>

<span class="nb">if</span><span class="p">(</span> <span class="s">NOT</span> <span class="s">LLVM_FOUND</span> <span class="p">)</span>
  <span class="nb">message</span><span class="p">(</span><span class="s">FATAL_ERROR</span> <span class="s2">&quot;LLVM package can&#39;t be found. Set CMAKE_PREFIX_PATH variable to LLVM&#39;s installation prefix.&quot;</span><span class="p">)</span>
<span class="nb">endif</span><span class="p">()</span>

<span class="nb">include_directories</span><span class="p">(</span> <span class="o">${</span><span class="nv">LLVM_INCLUDE_DIRS</span><span class="o">}</span> <span class="p">)</span>
<span class="nb">link_directories</span><span class="p">(</span> <span class="o">${</span><span class="nv">LLVM_LIBRARY_DIRS</span><span class="o">}</span> <span class="p">)</span>

<span class="nb">llvm_map_components_to_libraries</span><span class="p">(</span><span class="s">REQ_LLVM_LIBRARIES</span> <span class="s">jit</span> <span class="s">native</span><span class="p">)</span>

<span class="nb">target_link_libraries</span><span class="p">(</span><span class="s">mycompiler</span> <span class="o">${</span><span class="nv">REQ_LLVM_LIBRARIES</span><span class="o">}</span><span class="p">)</span>
</pre></div>
</div>
<div class="section" id="developing-llvm-pass-out-of-source">
<h3><a class="toc-backref" href="#id14">Developing LLVM pass out of source</a><a class="headerlink" href="#developing-llvm-pass-out-of-source" title="Permalink to this headline">¶</a></h3>
<p>It is possible to develop LLVM passes against installed LLVM.  An example of
project layout provided below:</p>
<div class="highlight-bash"><div class="highlight"><pre>&lt;project dir&gt;/
    |
    CMakeLists.txt
    &lt;pass name&gt;/
        |
        CMakeLists.txt
        Pass.cpp
        ...
</pre></div>
</div>
<p>Contents of <tt class="docutils literal"><span class="pre">&lt;project</span> <span class="pre">dir&gt;/CMakeLists.txt</span></tt>:</p>
<div class="highlight-cmake"><div class="highlight"><pre><span class="nb">find_package</span><span class="p">(</span><span class="s">LLVM</span><span class="p">)</span>

<span class="c"># Define add_llvm_* macro&#39;s.</span>
<span class="nb">include</span><span class="p">(</span><span class="s">AddLLVM</span><span class="p">)</span>

<span class="nb">add_definitions</span><span class="p">(</span><span class="o">${</span><span class="nv">LLVM_DEFINITIONS</span><span class="o">}</span><span class="p">)</span>
<span class="nb">include_directories</span><span class="p">(</span><span class="o">${</span><span class="nv">LLVM_INCLUDE_DIRS</span><span class="o">}</span><span class="p">)</span>
<span class="nb">link_directories</span><span class="p">(</span><span class="o">${</span><span class="nv">LLVM_LIBRARY_DIRS</span><span class="o">}</span><span class="p">)</span>

<span class="nb">add_subdirectory</span><span class="p">(</span><span class="s">&lt;pass</span> <span class="s">name&gt;</span><span class="p">)</span>
</pre></div>
</div>
<p>Contents of <tt class="docutils literal"><span class="pre">&lt;project</span> <span class="pre">dir&gt;/&lt;pass</span> <span class="pre">name&gt;/CMakeLists.txt</span></tt>:</p>
<div class="highlight-cmake"><div class="highlight"><pre><span class="nb">add_llvm_loadable_module</span><span class="p">(</span><span class="s">LLVMPassname</span>
  <span class="s">Pass.cpp</span>
  <span class="p">)</span>
</pre></div>
</div>
<p>When you are done developing your pass, you may wish to integrate it
into LLVM source tree. You can achieve it in two easy steps:</p>
<ol class="arabic simple">
<li>Copying <tt class="docutils literal"><span class="pre">&lt;pass</span> <span class="pre">name&gt;</span></tt> folder into <tt class="docutils literal"><span class="pre">&lt;LLVM</span> <span class="pre">root&gt;/lib/Transform</span></tt> directory.</li>
<li>Adding <tt class="docutils literal"><span class="pre">add_subdirectory(&lt;pass</span> <span class="pre">name&gt;)</span></tt> line into
<tt class="docutils literal"><span class="pre">&lt;LLVM</span> <span class="pre">root&gt;/lib/Transform/CMakeLists.txt</span></tt>.</li>
</ol>
</div>
</div>
<div class="section" id="compiler-platform-specific-topics">
<h2><a class="toc-backref" href="#id15">Compiler/Platform specific topics</a><a class="headerlink" href="#compiler-platform-specific-topics" title="Permalink to this headline">¶</a></h2>
<p>Notes for specific compilers and/or platforms.</p>
<div class="section" id="microsoft-visual-c">
<h3><a class="toc-backref" href="#id16">Microsoft Visual C++</a><a class="headerlink" href="#microsoft-visual-c" title="Permalink to this headline">¶</a></h3>
<dl class="docutils">
<dt><strong>LLVM_COMPILER_JOBS</strong>:STRING</dt>
<dd>Specifies the maximum number of parallell compiler jobs to use per project
when building with msbuild or Visual Studio. Only supported for Visual Studio
2008 and Visual Studio 2010 CMake generators. 0 means use all
processors. Default is 0.</dd>
</dl>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="HowToBuildOnARM.html" title="How To Build On ARM"
             >next</a> |</li>
        <li class="right" >
          <a href="userguides.html" title="User Guides"
             >previous</a> |</li>
  <li><a href="http://llvm.org/">LLVM Home</a>&nbsp;|&nbsp;</li>
  <li><a href="index.html">Documentation</a>&raquo;</li>

          <li><a href="userguides.html" >User Guides</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2012, LLVM Project.
      Last updated on 2012-12-21.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
    </div>
  </body>
</html>